package achi.community.irla.beans;

import achi.community.irla.database.Crypto;
import achi.community.irla.database.DatabaseImpl;
import achi.community.irla.database.ModelBean;
import achi.community.irla.entities.Achievements;
import achi.community.irla.entities.Users;
import com.icesoft.faces.context.effects.Effect;
import com.icesoft.faces.context.effects.Highlight;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.lang.String;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.FacesEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.icesoft.faces.component.selectinputtext.SelectInputText;

/**
 * RegControlBean: Validation for register.xhtml Adds the new Customer to DB and
 * ModelBean
 */
@ManagedBean
@RequestScoped
public class SearchControlBean implements Serializable {

    @EJB
    DatabaseImpl db;
    @ManagedProperty(value = "#{searchBackBean}")
    SearchBackBean backingBean;
    @ManagedProperty(value = "#{modelBean}")
    ModelBean modelBean;
    private List achiMatchPossibilities;
    // private Long choosenId;

    /**
     * public Long getChoosenId() { return choosenId; }
     *
     * public void setChoosenId(Long choosenId) { this.choosenId = choosenId; }
     */
    public List getAchiMatchPossibilities() {
        return achiMatchPossibilities;
    }

    public void setModelBean(ModelBean modelBean) {
        this.modelBean = modelBean;
    }

    public ModelBean getModelBean() {
        return modelBean;
    }

    public SearchBackBean getBackingBean() {
        return backingBean;
    }

    public void setBackingBean(SearchBackBean backingBean) {
        this.backingBean = backingBean;
    }

    public void updateList(ValueChangeEvent event) {

        updateSearch(event);
        // System.out.println("changed" + event);
        if (event.getComponent() instanceof SelectInputText) {
            SelectInputText autoComplete = (SelectInputText) event.getComponent();
            // if no selected item then return the previously selected item.
            String selected = event.getNewValue() + "";
            // REMOVE !!!????
            /**
             * System.out.println("\nEvent:"+event); System.out.println("\nEvent
             * new:"+event.getNewValue()); System.out.println("\nEvent
             * old:"+event.getOldValue());
             * System.out.println("\nEventcomponent:"+event.getComponent());
             */
            //System.out.println("\nautoComplete value:"+autoComplete.getSelectedItem().getValue());
            Achievements achi = modelBean.getAchievement(selected);
            if (achi != null) {
                backingBean.setAchi(achi);
            }
            /**
             * if (autoComplete.getSelectedItem() != null) {
             * System.out.println("\nIN\n"); Achievements currentAchi =
             * (Achievements) autoComplete.getSelectedItem().getValue();
             * backingBean.setAchi(currentAchi); System.out.println("\nachi is
             * set\n"); } // otherwise if there is a selected item get the value
             * from the match list else { Achievements currentAchi =
             * getMatch(autoComplete.getValue().toString()); if (currentAchi !=
             * null) { backingBean.setAchi(currentAchi); } }
             */
        }
        System.out.println("\nUpdate\n");

    }

    public void updateSearch(ValueChangeEvent event) {
        List<Achievements> aList = modelBean.getAllAchievements();
        //Achievements a = new Achievements("Achiname1", "Achiname2", null, null, null, null, null, 0);
        ArrayList list = new ArrayList();

        //System.out.println("event.getNewValue()"+event.getNewValue());
        String input = event.getNewValue().toString().toLowerCase();
        for (Achievements a : aList) {
            //Long id = a.getId();
            //choosenId = id;

            String title = a.getTitle_sv().toLowerCase();
            if (title.startsWith(input)) {
                SelectItem s = new SelectItem(a);
                list.add(s);
            }
        }
        achiMatchPossibilities = list;
    }

    public void selectedAchi(int event) {
        System.out.println("yessss: " + event);
    }
}